package com.quad.innovators.salesease.service.impl.warehouse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quad.innovators.salesease.common.utils.SystemCodeUtils;
import com.quad.innovators.salesease.converter.ArrivalOrderConverter;
import com.quad.innovators.salesease.converter.ArrivalOrderDetailConverter;
import com.quad.innovators.salesease.converter.PurchaseOrderDetailConverter;
import com.quad.innovators.salesease.enums.SystemCodeEnum;
import com.quad.innovators.salesease.mapper.warehouse.ArrivalOrderDetailMapper;
import com.quad.innovators.salesease.mapper.warehouse.PurchaseOrderDetailMapper;
import com.quad.innovators.salesease.model.entity.warehouse.ArrivalOrder;
import com.quad.innovators.salesease.model.entity.warehouse.ArrivalOrderDetail;
import com.quad.innovators.salesease.model.entity.warehouse.PurchaseOrderDetail;
import com.quad.innovators.salesease.model.form.warehouse.ArrivalOrderDetailForm;
import com.quad.innovators.salesease.model.form.warehouse.PurchaseOrderDetailForm;
import com.quad.innovators.salesease.service.warehouse.ArrivalOrderDetailService;
import com.quad.innovators.salesease.service.warehouse.PurchaseOrderDetailService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import lombok.RequiredArgsConstructor;

/**
 * @author : shanshan.li
 * @createTime : 2024/10/21 21:46
 * @Version : 1.0 
 */
@Service
@RequiredArgsConstructor
public class ArrivalOrderDetailServiceImpl extends ServiceImpl<ArrivalOrderDetailMapper, ArrivalOrderDetail> implements ArrivalOrderDetailService {

    private final ArrivalOrderDetailConverter arrivalOrderDetailConverter;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveList(String arrivalOrderCode, List<ArrivalOrderDetailForm> arrivalOrderDetailFormList) {
        return this.saveBatch(arrivalOrderDetailFormList.stream().map(model -> {
            ArrivalOrderDetail entity = arrivalOrderDetailConverter.toEntity(model);
            entity.setCode(SystemCodeUtils.getCode(SystemCodeEnum.PURCHASE_ORDER_DETAIL));
            entity.setArrivalCode(arrivalOrderCode);
            return entity;
        }).toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateList(String arrivalOrderCode, List<ArrivalOrderDetailForm> arrivalOrderDetailFormList) {
        int delete = this.baseMapper.delete(new QueryWrapper<ArrivalOrderDetail>().lambda().eq(ArrivalOrderDetail::getArrivalCode, arrivalOrderCode));
        return delete > 0 && saveList(arrivalOrderCode, arrivalOrderDetailFormList);
    }

    @Override
    public List<ArrivalOrderDetailForm> getListByArrivalOrderCode(String arrivalOrderCode) {
        return this.baseMapper.selectList(new LambdaQueryWrapper<ArrivalOrderDetail>().eq(ArrivalOrderDetail::getArrivalCode, arrivalOrderCode))
                .stream().map(arrivalOrderDetailConverter::toForm).toList();
    }
}
